import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;


/**
 * Created by DragonSea
 * on 2018-10-11
 */
public class Test2 {
    @Test
    //读取excel表格
    public void readExcel() {
        try {
            //获取文件输入流
            InputStream in=new FileInputStream("E:/test1.xls");
            //得到excel工作簿对象
            HSSFWorkbook wb = new HSSFWorkbook(in);
            //得到Excel工作表对象
            HSSFSheet sheetAt =wb.getSheetAt(0);
            //总行数
            int rowLength=sheetAt.getLastRowNum()+1;
            //获得excel的行
            Row row=sheetAt.getRow(0);
            //总列数
            int colLength = row.getLastCellNum();
            //获得工作表个数
            int sheetCount=wb.getNumberOfSheets();
            for(int i=0;i<rowLength;i++){
                Row row1=sheetAt.getRow(i);
                for(int j=0;j<colLength;j++){
                    HSSFCell hssfCell = (HSSFCell) row1.getCell(j);
                    if (hssfCell!= null) {
                        hssfCell.setCellType(CellType.STRING);
                    }

                    //获取每一列中的值
                    System.out.print(hssfCell.getStringCellValue() + "\t");
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
